import axios from "axios";


// 获取网页中的图片链接
export async function GetImages(baseUrl:string){
    // 存储图片 URL 的数组
    let urlsArray;

    try {
      const response = await axios.get(baseUrl);

      // 提取 PNG 图片的链接
      const html = response.data;
      const urls = [...html.matchAll(/href="([^"]+\.png)"/g)].map(
        (match) => baseUrl + match[1]
      );

      if (urls.length > 0) {
        urlsArray = urls;
      }
    } catch (error) {
      console.error("获取图片失败:", error);
      urlsArray = [];

    }

    return urlsArray;
};


// 获取链接中的所有文件链接
export async function GetFileURL(baseUrl:string,fileSuffix:string){
  let urlsArray;

  try {
    const response = await axios.get(baseUrl);

    const html = response.data;

    let urls;
    // NOTE:暂时只支持以下内容
    switch(fileSuffix){
      case ".png":
        urls = [...html.matchAll(/href="([^"]+\.png)"/g)].map(
          (match) => baseUrl + match[1]
        );
        break;
      case ".glb":
        urls = [...html.matchAll(/href="([^"]+\.glb)"/g)].map(
          (match) => baseUrl + match[1]
        );
        break;
      case ".DAT":
        urls = [...html.matchAll(/href="([^"]+\.DAT)"/g)].map(
          (match) => baseUrl + match[1]
        );
        break;
    }
    // console.log("urls:", urls);

    if (urls.length > 0) {
      urlsArray = urls;
    }

  } catch (error) {
    console.error("获取图片失败:", error);
    urlsArray = [];

  }

  return urlsArray;
};


export async function GetURLContent(url:string) {
  try {
    // 使用 Axios 获取文件内容
    const response = await axios.get(url, { responseType: "text" });
    const fileContent = response.data;

    // 在控制台输出内容
    // console.log("文件内容:", fileContent);

    return fileContent;

  } catch (error) {
    console.error("下载或解析文件时出错:", error);
  }
};
